package cn.com.doone.common.uc.web.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.doone.common.uc.utils.MessageUtils;
import cn.com.doone.common.uc.utils.StringUtils;
import cn.com.doone.common.uc.web.dao.SystemDao;

@Service
public class SystemService {

	@Autowired
	private SystemDao systemDao;
	
	/**
	 * 查询分页列表
	 * @param paramsMap
	 * @return
	 */
	public Map<String,Object> querySysCodePageList(Map<String,Object> paramsMap) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int page = Integer.parseInt(paramsMap.get("page").toString());
		int limit = Integer.parseInt(paramsMap.get("limit").toString());
		paramsMap.put("start", (page - 1) * limit);
		paramsMap.put("limit", limit);
		List<Map<String,Object>> sysCodeList = systemDao.querySysCodePageList(paramsMap);
		long count = systemDao.countSysCodePageList(paramsMap);
		resultMap.put("success", true);
		resultMap.put("total", count);
		resultMap.put("data", sysCodeList);
		return resultMap;
	}
	
	/**
	 * 添加或者修改
	 * @param paramsMap
	 * @return
	 */
	public Map<String,Object> mergeDict(Map<String,Object> paramsMap) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//判断value是否重复
		if(!this.validCodeNameAndValue(paramsMap)) {
			resultMap.put("success", false);
			resultMap.put("message", "字典值重复");
			return resultMap;
		}
		paramsMap.put("status", 1);
		if(StringUtils.isNull(paramsMap.get("id"))) {
			systemDao.insert(paramsMap);
		} else {
			systemDao.update(paramsMap);
		}
		resultMap.put("success", true);
		resultMap.put("message", MessageUtils.DO_SUCCESS);
		return resultMap;
	}
	
	/**
	 * 改变字典状态
	 * @param paramsMap
	 * @return
	 */
	public Map<String,Object> changeDictStatus(Map<String,Object> paramsMap) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> tmpMap = new HashMap<String,Object>();
		tmpMap.put("id", paramsMap.get("id"));
		tmpMap.put("status", paramsMap.get("status"));
		if("1".equals(tmpMap.get("status").toString())) {
			//查询字典信息
			Map<String,Object> dictMap = systemDao.queryDictMapById(tmpMap.get("id").toString());
			if(StringUtils.isNull(dictMap) || dictMap.isEmpty()) {
				resultMap.put("success", false);
				resultMap.put("message", "字典信息为空");
				return resultMap;
			}
			//判断value是否重复
			if(!this.validCodeNameAndValue(dictMap)) {
				resultMap.put("success", false);
				resultMap.put("message", "字典值重复");
				return resultMap;
			}
		}
		systemDao.update(tmpMap);
		resultMap.put("success", true);
		resultMap.put("message", MessageUtils.DO_SUCCESS);
		return resultMap;
	}
	
	/**
	 * 验证字典值
	 * @param paramsMap
	 * @return
	 */
	private boolean validCodeNameAndValue(Map<String,Object> paramsMap) {
		Map<String,Object> tmpMap = new HashMap<String,Object>();
		tmpMap.put("typeName", paramsMap.get("typeName"));
		tmpMap.put("value", paramsMap.get("value"));
		tmpMap.put("status", 1);
		long count = systemDao.countTypeNameAndValue(tmpMap);
		if(count > 0) {
			if(StringUtils.isNull(paramsMap.get("id"))) {
				return false;
			} else {
				tmpMap.put("sysCodeId", paramsMap.get("id"));
				count = systemDao.countTypeNameAndValue(tmpMap);
				if(count == 0) {
					return false;
				}
			}
		}
		return true;
	}
}
